package me.numark;

import com.google.gson.Gson;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.kyleduo.switchbutton.SwitchButton;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import me.numark.model.ProfileResponse;
import me.numark.model.User;
import me.numark.net.GsonRequest;
import me.numark.net.NetworkConstants;
import me.numark.net.NetworkHelper;
import me.numark.utils.Constants;
import me.numark.utils.Settings;

/**
 * Created by tian on 14-9-20:下午10:25.
 */
public class SettingActivity extends AuthenticatorActivity implements View.OnClickListener,
        CompoundButton.OnCheckedChangeListener {

    @InjectView(R.id.back)
    View back;

    @InjectView(R.id.title)
    TextView title;

    @InjectView(R.id.right_text)
    View rightView;

    @InjectView(R.id.received_comments_switch)
    SwitchButton commentsSwitch;

    @InjectView(R.id.forward_switch)
    SwitchButton forwardSwitch;

    @InjectView(R.id.at_switch)
    SwitchButton atSwitch;

    @InjectView(R.id.new_message_interval)
    View messageInterval;

    @InjectView(R.id.contacts)
    TextView contacts;

    @InjectView(R.id.weibo)
    TextView weibo;

    @InjectView(R.id.about_app)
    View about;

    @InjectView(R.id.reset_password)
    View resetPassword;

    @InjectView(R.id.log_out)
    View logOut;

    private User mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        mUser = new Gson().fromJson(Settings.getProfile(), User.class);
        ButterKnife.inject(this);
        back.setOnClickListener(this);
        title.setText(R.string.common_settings);
        bindData();
        rightView.setVisibility(View.INVISIBLE);
        commentsSwitch.setOnCheckedChangeListener(this);
        forwardSwitch.setOnCheckedChangeListener(this);
        atSwitch.setOnCheckedChangeListener(this);
        messageInterval.setOnClickListener(this);
        contacts.setOnClickListener(this);
        weibo.setOnClickListener(this);
        about.setOnClickListener(this);
        resetPassword.setOnClickListener(this);
        logOut.setOnClickListener(this);
    }

    private void bindData() {
        forwardSwitch.setChecked(mUser.is_forward);
        commentsSwitch.setChecked(mUser.is_comment);
        atSwitch.setChecked(mUser.is_at);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.new_message_interval:
                int interval = -1;
                try {
                    interval = Integer.parseInt(mUser.message_interval);
                } catch (Exception e) {

                }
                Intent intent = new Intent(this, MessageIntervalActivity.class);
                intent.putExtra(Constants.MESSAGE_INTERVAL_EXTRA, interval);
                startActivity(intent);
                break;
            case R.id.contacts:
                break;
            case R.id.weibo:
                break;
            case R.id.about_app:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case R.id.reset_password:
                break;
            case R.id.log_out:
                Settings.logOut();
                LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Constants.INTENT_LOG_OUT));
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.received_comments_switch:
                setMessageReminder();
                break;
            case R.id.forward_switch:
                setMessageReminder();
                break;
            case R.id.at_switch:
                setMessageReminder();
                break;
            default:
                break;
        }
    }

    private void setMessageReminder() {
        String url = String.format(NetworkConstants.MESSAGE_REMIND_URL, Settings.getUid());
        Map<String, String> body = new HashMap<String, String>();
        body.put("is_comment", Boolean.toString(commentsSwitch.isChecked()));
        body.put("is_forward", Boolean.toString(forwardSwitch.isChecked()));
        body.put("is_at", Boolean.toString(atSwitch.isChecked()));
        GsonRequest<ProfileResponse> request = new GsonRequest<ProfileResponse>(url,
                Request.Method.POST, ProfileResponse.class, true, body,
                new Response.Listener<ProfileResponse>() {
                    @Override
                    public void onResponse(ProfileResponse response) {
                        dismissProgress();
                        if (response.ret) {
                            Settings.saveProfile(response.data.toString());
                            mUser = response.data;
                            bindData();
                        } else {
                            Toast.makeText(SettingActivity.this, response.msg, Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dismissProgress();
                Toast.makeText(SettingActivity.this, R.string.generic_network_error,
                        Toast.LENGTH_SHORT).show();
            }
        });
        showProgress();
        NetworkHelper.getInstance().addToRequestQueue(request);
        NetworkHelper.getInstance().getRequestQueue().start();
    }
}
